<?php

namespace App\Kuafu\Nsq\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqPublisher;


/**
 * @description NSQ 通用队列推送
 *
 * @queueName {RMS-NSQ-SEND-QUEUE}
 */
class NsqSendJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 队列名称
     *
     * @var string
     */
    const QUEUE_NAME = '{RMS-NSQ-SEND}';

    private string $topic;

    private array $body;

    /**
     * @param string $topic
     * @param array $body
     */
    public function __construct(string $topic, array $body)
    {
        $this->topic = $topic;
        $this->body  = $body;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info('NSQ SendJob handle');
        // 业务逻辑
        $this->body['nsqEventTime'] = time();

        (new NsqPublisher(config('nsq.default.address')))->pub($this->topic, $this->body);

        Log::info('NSQ SendJob Send Data', ['topic' => $this->topic, 'body' => $this->body]);
    }
}
